#include "binfile.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QFile>
#include <QDataStream>
#include <QDebug>

quint32 BinFile::m_appVer = 0; // 软件版本
QByteArray BinFile::m_operatorMsg;  // 操作员信息
quint32 BinFile::m_rdFileLen = 0; // 读出来的文件长度
QByteArray BinFile::m_fileArr;

BinFile::BinFile(Ui::MainWindow *pui,QWidget *parent)
    : QMainWindow{parent}
{
    ui = pui;

    connect(ui->BtnFilenameSel, &QPushButton::clicked, this, &BinFile::onBtnFilenameSelClicked);
}

BinFile::~BinFile()
{

}

quint32 &BinFile::getAppVer()
{
    return m_appVer;
}

QByteArray &BinFile::getOperatorMsg()
{
    return m_operatorMsg;
}

quint32 &BinFile::getRdFileLen()
{
    return m_rdFileLen;
}

QByteArray BinFile::getBinFileMid(qsizetype index, qsizetype len)
{
    return m_fileArr.mid(index,len);
}

QByteArray &BinFile::getFileArr()
{
    return m_fileArr;
}

void BinFile::onBtnFilenameSelClicked()
{
    m_filename = QFileDialog::getOpenFileName(
        this,
        tr("open a file."),
        "E:/",
        tr("boot file(*.boot);;Binary files(*.bin *.raw);;symbols(*.srec *.s19);;All files(*.*)"));
    if (m_filename.isEmpty())
    {
        QMessageBox::warning(this, "Warning!", "filename is empty");
    }
    else
    {
        ui->LineEdtFilename->setText(m_filename);
        QFile file(m_filename);

        if(!file.open(QIODevice::ReadOnly))
        {
            QMessageBox::warning(this, "error!", "Failed to open the file!");
            return;
        }
        m_fileArr = file.readAll();
        file.close();

        // 分离信息
        getUpdateMessage();

        m_fileLen = m_fileArr.length();
        QString str1 = QString("文件长度：%1 Bytes").arg(QString::number(m_fileLen));
        ui->txtLabFile->setText(str1);

        QString str2;
        for(qsizetype i = 0; i < m_fileLen; i++)
        {
            if(i % 16 == 0)
            {
                if(i)
                {
                    ui->textEditBin->append(str2);
                }
                //
                str2 = QStringLiteral("%1\t\t").arg(i, 8, 16, QLatin1Char('0')).toUpper();
            }
            else if(i % 8 == 0)
            {
                str2 += "\t";
            }
            else
            {
                str2 += " ";
            }
            str2 += QString("%1").arg(m_fileArr.at(i)&0xff,2,16, QLatin1Char('0')).toUpper();
        }
        ui->textEditBin->append(str2);

        if(m_rdFileLen != m_fileLen)
        {
            QMessageBox::critical(this, "error!", "The BIN file is damaged!");
        }
    }
}

void BinFile::getUpdateMessage()
{
    QByteArray appver = m_fileArr.left(16);
    m_appVer = appver.toUInt();
    m_fileArr.remove(0,16);

    m_operatorMsg =  m_fileArr.left(32);
    m_fileArr.remove(0,32);
    for(qsizetype i = 0; i < m_operatorMsg.size(); i++)
    {
        if(!m_operatorMsg.at(i))
        {
            m_operatorMsg.removeAt(i);
            if(i) i--;
        }
    }
    QString opmsg(m_operatorMsg);

    QByteArray readl = m_fileArr.left(16);
    m_rdFileLen = readl.toUInt();
    m_fileArr.remove(0,16);

    QString str = QString("app version: %1, operator: \"%2\", BIN len: %3")
                      .arg(m_appVer)
                      .arg(opmsg)
                      .arg(m_rdFileLen);
    ui->txtLabMsg->setText(str);
}


